<template>
	<view class="container">
		<button :disabled="startBtnDisabled" @click="startRecord">开始录音</button>
		<button :disabled="endBtnDisabled" @click="stopRecord">结束录音</button>
		<button @click="playRecord">播放录音</button>
		<button @click="uploadRecod">上传录音</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startBtnDisabled:false,
				endBtnDisabled:true,
				//录音管理器
				recorderManager:null,
				//音频播放组件
				innerAudioContext:null,
				//录音文件地址
				tempRecordPath:null
			};
		},
		methods:{
			//开始录音
			startRecord(){
				this.recorderManager.start({
					format:'mp3',
					duration:300000					
				})				
			},
			//停止录音
			stopRecord(){
				this.recorderManager.stop();
			},
			//播放录音
			playRecord(){
				if(this.tempRecordPath){
				this.innerAudioContext.src = this.tempRecordPath;
				this.innerAudioContext.play();
				}
			},
			//上传录音
			uploadRecod(){
				uni.uploadFile({
					url:'http://localhost:8080/interview/answer/testUploadRecord',
					filePath:this.tempRecordPath,
					name:'file',
					formData:{
						questionId:10
					},
					success: (res) => {
						console.log('res',res)
					}
					
				})
			}
			
		},
		onLoad() {
			//页面加载即得到录音管理器对象
			this.recorderManager=uni.getRecorderManager();
			this.innerAudioContext = uni.createInnerAudioContext();
			//开始的时候
			this.recorderManager.onStart(()=>{
				this.startBtnDisabled=true
				this.endBtnDisabled=false
				console.log("开始录音........")
				
			});
			
			//结束的时候
			this.recorderManager.onStop((res)=>{
				this.endBtnDisabled=true
				this.startBtnDisabled=false
				//赋值
				this.tempRecordPath=res.tempFilePath
				console.log("录音完成........")
				console.log('tempFilePath',res.tempFilePath)
			})
		}
	}
</script>

<style lang="scss">
	.container{
		padding: 30rpx;
		button{
			margin: 30rpx;
		}
	}
</style>